import Taro from '@tarojs/taro'
import { responseInterceptor, requestInterceptor } from '../api/interceptor/index.js'
const baseURL = process.env.TARO_APP_API_URL

  // 打印日志 --部分 node环境下的process中的obj值编译成微信小程序语法后，没有重新赋值转换 question
  // console.log('加载-baseurl',process.env.TARO_APP_API_URL)

Taro.addInterceptor(Taro.interceptors.logInterceptor)
Taro.addInterceptor(requestInterceptor)

const request = async (url, options = {}) => {
  const token = Taro.getStorageSync('token')
  const defaultOptions = {
    url: baseURL + url,
    method: 'GET',
    header: {
      'Content-Type': 'application/json',
      "Token":token
      // 'Authorization': token ? `Bearer ${token}` : '',
    },
    ...options,
  }
  const response = await Taro.request(defaultOptions)
  return responseInterceptor(response)
}

const get = (url, params = {}) => {
  return request(url, { method: 'GET', data: params })
}

const post = (url, data = {}) => {
  return request(url, { method: 'POST', data })
}

const put = (url, data = {}) => {
  return request(url, { method: 'PUT', data })
}

const del = (url, data = {}) => {
  return request(url, { method: 'DELETE', data })
}

export default {
  get,
  post,
  put,
  delete: del,
}